package org.jeecg.modules.erp.goods.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.modules.erp.goods.entity.ErpGoodsSupplier;
import org.jeecg.modules.erp.goods.service.IErpGoodsSupplierService;
import org.jeecg.modules.erp.goods.vo.ErpGoodsSupplierVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Api(tags = "物料供应商")
@RestController
@RequestMapping("/base/erpGoodsSupplier")
@Slf4j
public class ErpGoodsSupplierController {

	@Autowired
	private IErpGoodsSupplierService erpGoodsSupplierService;
	
	/**
	 * @功能：查询供应商
	 * @param erpGoodsSupplier
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<IPage<ErpGoodsSupplierVo>> queryPageList(ErpGoodsSupplierVo erpGoodsSupplier, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
		Result<IPage<ErpGoodsSupplierVo>> result = new Result<IPage<ErpGoodsSupplierVo>>();
//		QueryWrapper<ErpGoodsSupplierVo> queryWrapper = QueryGenerator.initQueryWrapper(erpGoodsSupplier, req.getParameterMap());
//		queryWrapper.orderByAsc("sort_order");
		Page<ErpGoodsSupplierVo> page = new Page<ErpGoodsSupplierVo>(pageNo, pageSize);
		IPage<ErpGoodsSupplierVo> pageList = erpGoodsSupplierService.myPage(page, erpGoodsSupplier);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	
	/**
	 * @功能：新增
	 * @return
	 */
	@RequiresRoles({"admin"})
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result<ErpGoodsSupplier> add(@RequestBody ErpGoodsSupplierVo erpGoodsSupplier,HttpServletRequest req) {
		Result<ErpGoodsSupplier> result = new Result<ErpGoodsSupplier>();
		try {
			erpGoodsSupplierService.addSave(erpGoodsSupplier,req);
			result.success("保存成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}

	/**
	 * @功能：删除供应商
	 * @param id
	 * @return
	 */
	//@RequiresRoles({"admin"})
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	@CacheEvict(value={CacheConstant.SYS_DICT_CACHE, CacheConstant.SYS_ENABLE_DICT_CACHE}, allEntries=true)
	public Result<ErpGoodsSupplier> delete(@RequestParam(name="id",required=true) String id) {
		Result<ErpGoodsSupplier> result = new Result<ErpGoodsSupplier>();
		ErpGoodsSupplier joinSystem = erpGoodsSupplierService.getById(id);
		if(joinSystem==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = erpGoodsSupplierService.removeById(id);
			if(ok) {
				result.success("删除成功!");
			}
		}
		return result;
	}

	/**
	 * @功能：批量删除供应商
	 * @param ids
	 * @return
	 */
	//@RequiresRoles({"admin"})
	@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
	@CacheEvict(value={CacheConstant.SYS_DICT_CACHE, CacheConstant.SYS_ENABLE_DICT_CACHE}, allEntries=true)
	public Result<ErpGoodsSupplier> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<ErpGoodsSupplier> result = new Result<ErpGoodsSupplier>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.erpGoodsSupplierService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}

}
